using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 管理每一位顾客的到来->诊断->策略 …… 等阶段
/// </summary>
public class GuestManager : Singleton<GuestManager>
{
    public string fileName = "Guests";
    private GuestList guestList;   

    private List<Guest> guests  = new List<Guest>();     //加载到这个角色列表

    public int GuestCount { get { return guests.Count; } }

    private void Awake()
    {
        LoadGuests();
    }

    /// <summary>
    /// 加载顾客Json
    /// </summary>
    public void LoadGuests()
    {
        TextAsset jsonFile = Resources.Load<TextAsset>(fileName);

        if (jsonFile != null)
        {
            guestList = JsonUtility.FromJson<GuestList>(jsonFile.text);
            Debug.Log("Guest data loaded successfully! Count: " + guestList.guests.Count);
            //print(guestList.guests[currentIndex].name);

            foreach (var g in guestList.guests)
            {
                Guest guest = new Guest(g);
                guests.Add(guest);
            }
        }
    }

    /// <summary>
    /// 获取角色列表
    /// </summary>
    /// <returns></returns>
    public GuestList GetGuestList()
    {
        return guestList;
    }

    /// <summary>
    /// 获取角色对应阶段的数据，具体可获得见Stage
    /// </summary>
    /// <param name="guestId">角色id</param>
    /// <param name="stageIndex"></param>
    /// <returns></returns>
    public GuestStage GetStage(int guestId, GameState curState)
    {
        int stageIndex;
        switch(curState)
        {
            case GameState.FirstTurn:
                stageIndex = 0;
                break;
            case GameState.SecondTurn:
                stageIndex = 1;
                break;

            default:
                stageIndex = 0;
                break;
        }

        var guest = GetGuest(guestId);
        if (guest == null || stageIndex >= guest.stages.Count)
            return null;
        return guest.stages[stageIndex];
    }

    /// <summary>
    /// 获取角色对应阶段的数据，具体可获得见Stage
    /// </summary>
    /// <param name="guest">角色</param>
    /// <param name="curState">当前阶段</param>
    /// <returns></returns>
    public GuestStage GetStage(Guest guest, GameState curState)
    {
        int stageIndex;
        switch (curState)
        {
            case GameState.FirstTurn:
                stageIndex = 0;
                break;
            case GameState.SecondTurn:
                stageIndex = 1;
                break;

            default:
                stageIndex = 0;
                break;
        }

        if (guest == null || stageIndex >= guest.stages.Count)
            return null;
        return guest.stages[stageIndex];
    }

    /// <summary>
    /// 进入下一个角色
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public Guest EnterNextGuest(ref int index)
    {
        index++;
        if (index >= guests.Count)
        {
            return null;
        }
        return guests[index];
    }

    /// <summary>
    /// 根据id获取对应角色
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public Guest GetGuest(int index)
    {
        return guests[index];
    }
}
